package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.AddressDao;
import com.qf.xiaomi.pojo.TbAddress;
import com.qf.xiaomi.utils.DruidDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/20 10:57
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class AddressDaoImpl implements AddressDao {

    /**
     * 根据传入参数增加地址信息
     * @param id 地址id
     * @param name 收件人名称
     * @param phone 收件人电话
     * @param detail 收件地址
     * @return
     */

    //新增地址
    @Override
    public int addAddressInfo(int id, String name, String phone, String detail) {
        String sql="insert into tb_address(uid,name,phone,detail) values(?,?,?,?)";
        QueryRunner queryRunner=new QueryRunner();
        final int addResult;
        try {
            addResult = queryRunner.update(DruidDButils.getDruidConn(), sql, id, name, phone, detail);
            if (addResult>0)
                return addResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据地址id与用户id设置默认值
     * @param id
     * @param userId
     * @return
     */
    @Override
    public int deleteAddressInfo(int id, int userId) {
        //根据用户id与地址id删除地址信息
        String sql="delete from tb_address  where id=? and uid=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final int deletResult = queryRunner.update(DruidDButils.getDruidConn(), sql, id, userId);
            return deletResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * 根据地址id与用户id设置默认值
     * @param id
     * @param userId
     * @return
     */
    @Override
    public int setDefaultAddressInfo(int id, int userId) {

        String sql="update  tb_address t set level=1 where t.uid=? and t.id=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final int updateResult = queryRunner.update(DruidDButils.getDruidConn(), sql, userId, id);
            return  updateResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询是否存在默认值地址
     * @param userId
     * @return
     */
    @Override
    public int queryDefaultAddressInfo(int userId) {
        String sql = "select count(0) from tb_address t where t.uid=? and t.level=1";
        QueryRunner queryRunner = new QueryRunner();
        try {
            final Object query = queryRunner.query(DruidDButils.getDruidConn(), sql, new ScalarHandler(), userId);
            int qurestResult = Integer.parseInt(String.valueOf(query));
            return qurestResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据用户id将原设置的默认地址设置为普通地址
     * @param userId
     * @return
     */
    public int udateDefaultAddressInfo(int userId) {
        String sql = "update  tb_address  set level= 0 where uid=? and level=1";
        QueryRunner queryRunner = new QueryRunner();
        try {
            final int updateResult =queryRunner.update(DruidDButils.getDruidConn(),sql,userId);
            return updateResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改地址信息
     * @param id
     * @param userId
     * @return
     */
    @Override
    public int updateAddressInfo(int id,String name,String phone, String detail,int userId) {
        String sql = "update  tb_address set name=?,phone=?,detail=? where uid=? and id=?";
        QueryRunner queryRunner = new QueryRunner();
        try {
            final int updateResult = queryRunner.update(DruidDButils.getDruidConn(), sql,name,phone,detail,userId,id);
            return updateResult;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据用户ID查询地址信息并返回一个List集合
     * @param userId
     * @return
     */
    @Override
    public List<TbAddress> queryAddressInfoByUserId(int userId) {
            String sql="SELECT  t.id,  t.detail,  t.name,  t.phone,  t.uid, t.level,  t.state FROM db_shopping.tb_address t WHERE t.uid=?";
            QueryRunner queryRunner=new QueryRunner();
         List<TbAddress> addressList=null;
        try {
            addressList = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanListHandler<>(TbAddress.class), userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return addressList;
    }

}
